package com.example.springsecurity.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import com.example.springsecurity.mapper.UserMapper;
import com.example.springsecurity.pojo.LoginVO;
import com.example.springsecurity.pojo.MyUser;
import com.example.springsecurity.pojo.RegisterRequest;
import com.example.springsecurity.pojo.Result;
import com.example.springsecurity.service.LoginServiceImpl;
import com.example.springsecurity.utils.SMSUtil;
import jakarta.annotation.Resource;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
import java.util.concurrent.TimeUnit;

@RestController
@CrossOrigin
public class LoginController {
    @Resource
    private LoginServiceImpl loginService;
    @Resource
    private RedisTemplate redisTemplate;
    @Resource
    private SMSUtil smsUtil;
    @Resource
    private UserMapper userMapper;


    @RequestMapping("test1")
    public String test1() {
        return "hello";
    }

    //登录接口
    @RequestMapping("/login")
    public Result login(@RequestBody LoginVO loginVO) {
        //获取请求参数

        Object code = redisTemplate.opsForValue().get("code");
        if (code == null) {
            return new Result(401,"验证码已过期或未生成",null);
        }
        Result result = loginService.login(loginVO);
        return result;
    }
    //获取图形验证码接口
    @RequestMapping("/cap")
    public Result cap() {
        LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(200, 100, 4, 10);
        // 获取验证码
        String code = lineCaptcha.getCode();
        //redis存储验证码
        redisTemplate.opsForValue().set("code", code);//以key，value形式存储到redis中
        //设置验证码在redis中的有效期为30s
        redisTemplate.expire("code", 20, TimeUnit.SECONDS);
        //返回一个base64编码的图片json字符串
        String imageBase64Data = lineCaptcha.getImageBase64Data();

        return new Result(200, "成功获取验证码", imageBase64Data);
    }






}
